package com.resou.reader.utils;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.LargessBook;
import com.resou.reader.bookshelf.readhistory.datasupport.BookCollection;

import java.util.List;

public class TextUtils {

    /**
     * 判断一个对象是否为null
     *
     * @param t
     * @param <OBJ>
     * @return
     */
    public static <OBJ> boolean isEmpty(OBJ t) {
        try {
            if (null == t) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断一个集合是否为null
     *
     * @param o
     * @param <OBJ>
     * @return
     */
    public static <OBJ> boolean isEmpty(List<OBJ> o) {
        try {
            if (null == o || o.size() == 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 计算字数
     *
     * @return
     */
    public static String caclFontNum(Integer integer) {
        float num = 10000;
        if (null != integer) {
            if (integer > num) {
                return Float.valueOf(integer / num) + "万字";
            } else {
                return integer + "字";
            }
        }
        return "0字";
    }

    /**
     * 格式化文本
     *
     * @param info
     * @return
     */
    public static String formatText(String info) {
        if (!android.text.TextUtils.isEmpty(info)) {
            return info.replaceAll("\\r\\n", "").replaceAll(" ", "").trim();
        } else {
            return "";
        }
    }

    /***
     * 设置字体颜色
     * @param textView
     * @param text
     * @param sourceText
     */
    public static void setTimeColor(TextView textView, String text, String sourceText) {
        SpannableStringBuilder sbb = new SpannableStringBuilder(text);
        for (int i = 0; i < text.length(); i++) {
            String temp = text.substring(i, i + 1);
            if (sourceText.contains(temp)) {
                sbb.setSpan(new ForegroundColorSpan(textView.getResources().getColor(R.color.theme_color)),
                        i, i + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);//Color.parseColor("#ffc36a")
            }
        }
        textView.setText(sbb);
    }

    /**
     * 设置下滑线
     *
     * @param view
     * @param info
     */
    public static void setUnderLineSpan(TextView view, String info) {
        SpannableString content = new SpannableString(info);
        content.setSpan(new UnderlineSpan(), 0, info.length(), 0);
        view.setText(content);
    }

    public static String getFreeBooks(List<BookCollection> mObjects) {
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < mObjects.size(); i++) {
            Object o = mObjects.get(i);
            if (o != null) {
                BookCollection book = (BookCollection) o;
                int freeStatus = book.getFreeStatus();
                if (freeStatus == 0) {
                    buffer.append(book.getBookId()).append("_").append(0);
                } else {
                    buffer.append(book.getBookId()).append("_").append(1);
                }
                if (i != mObjects.size() - 1) {
                    buffer.append(",");
                }
            }
        }
        return buffer.toString();
    }

    public static String getFreeBooks2(List<LargessBook> mObjects) {
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < mObjects.size(); i++) {
            Object o = mObjects.get(i);
            if (o != null) {
                LargessBook book = (LargessBook) o;
                int freeStatus = book.getFreeStatus();
                if (freeStatus == 0) {
                    buffer.append(book.getBookId()).append("_").append(0);
                } else {
                    buffer.append(book.getBookId()).append("_").append(1);
                }
                if (i != mObjects.size() - 1) {
                    buffer.append(",");
                }
            }
        }
        return buffer.toString();
    }
}
